// JavaScript Document
var password_save = "";
var bool = false;

function validateNonEmpty(inputField, helpText) {
	// See if the input value contains any text
	return validateRegEx(/.+/, inputField.value, helpText, " 请填写");
}
function changeBool() {
	bool = false;
}

function validateRegEx(regex, input, helpText, helpMessage, name) {
	// See if the input data validates OK
	if (!input) {
		//document.getElementById(name).className = "denied";
		return false;
	} else if (!regex.test(input)) {
		// The data is invalid, so set the help message and return false
		if (helpText != null) {
			helpText.innerHTML = helpMessage;
			document.getElementById(name).className = "denied";
		}
		return false;
	} else {
		// The data is OK, so clear the help message and return true
		if (helpText != null) {
			helpText.innerHTML = "";
			//alert(name);
			if (name != "img_username")
				document.getElementById(name).className = "approved";
		}
		return true;
	}
}

function validateName(minLength, maxLength, inputField, helpText) {
	// See if the input value contains at least minLength but no more than maxLength characters
	if (validateLength(minLength, maxLength, inputField, helpText))
		checkUsername();
}

function validateLength(minLength, maxLength, inputField, helpText, name) {
	// See if the input value contains at least minLength but no more than maxLength characters
	return validateRegEx(
			new RegExp("^.{" + minLength + "," + maxLength + "}$"),
			inputField.value, helpText, " 名称长度为 " + minLength + " 到 "
					+ maxLength + " 位", "img_username");
}

function validatePassword(minLength, maxLength, inputField, helpText) {
	// See if the input value contains at least minLength but no more than maxLength characters
	password_save = inputField.value;
	return validateRegEx(
			new RegExp("^.{" + minLength + "," + maxLength + "}$"),
			inputField.value, helpText, " 密码长度为 " + minLength + " 到 "
					+ maxLength + " 位", "img_password");
}

function validateSame(re_password, helpText) {
	if (re_password.value != password_save) {
		if (helpText != null) {
			while (helpText.firstChild)
				helpText.removeChild(helpText.firstChild);
			helpText.appendChild(document.createTextNode(" 两次密码输入不相同"));
			document.getElementById("img_re_password").className = "denied";
		}
		return false;
	} else {
		if (helpText != null) {
			while (helpText.firstChild)
				helpText.removeChild(helpText.firstChild);
			helpText.appendChild(document.createTextNode(""));
			document.getElementById("img_re_password").className = "approved";
		}
		return true;
	}
}

function validatePhone(inputField, helpText) {
	// First see if the input value contains data
	//if (!validateNonEmpty(inputField, helpText))
	//return false;

	// Then see if the input value is a phone number
	return validateRegEx(/^\d{11}$/, inputField.value, helpText, "请输入正确的电话号码",
			"img_phone");
}

function validateEmail(inputField, helpText) {
	//if (!validateNonEmpty(inputField, helpText))
	//return false;

	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,3})+$/, inputField.value,
			helpText, " 请输入正确的邮箱格式", "img_email");
}

function checkUsername() {
	document.getElementById("img_username").className = "thinking";
	request = createRequest();
	if (request == null) {
		alert("Unable to create request!");
	} else {
		var theName = document.getElementById("username").value;
		var username = escape(theName);

		//var arguments = "<username>" + username + "</username>";
		var arguments = "";
		arguments = addTag(arguments, "username", username);
		var url = "TestUserName";

		sendXMLPost(url, arguments, showUsernameStatus);
	}
}

function showUsernameStatus(responseXML) {
	var resp;
	var result = responseXML.getElementsByTagName("result");
	resp = result[0].firstChild.nodeValue;

	if (resp == "okay") {
		// No error.
		document.getElementById("img_username").className = "approved";
		bool = true;
	} else {
		document.getElementById("img_username").className = "denied";
		bool = false;
	}
}

function register() {
	if(bool &&
			   validatePassword(8, 20, document.getElementById("password"), document.getElementById("password_help")) &&
			   validateSame(document.getElementById("re_password"), document.getElementById("same_help")) &&
			   validatePhone(document.getElementById("phone"), document.getElementById("phone_help")) &&
			   validateEmail(document.getElementById("email"), document.getElementById("email_help"))){

		var arguments = "<user>";
		arguments = addTag(arguments, "name", document
				.getElementById("username").value);
		arguments = addTag(arguments, "password", document
				.getElementById("password").value);
		arguments = addTag(arguments, "phone",
				document.getElementById("phone").value);
		arguments = addTag(arguments, "email",
				document.getElementById("email").value);
		arguments = addTag(arguments, "sex",
				document.getElementById("sex").value);
		arguments = addTag(arguments, "describe",
				document.getElementById("describe").value);
		arguments = addTag(arguments, "image",
				document.getElementById("personimage").value);
		arguments += "</user>";
		var url = "UserRegister";
		sendXMLPost(url, arguments, null);
		alert("注册成功！");
		window.location="index.html";
	} else {
		alert("?");
	}
}

function submitImageButton(){
	document.getElementById("fileField").click();
}

function clickSubmit() {
	document.getElementById("ImageSubmitButton").click();
}

function setImagePath(path) {
	document.getElementById("personimage").value = path;
}